package com.rupeng.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Card;
import com.rupeng.pojo.CardSubject;
import com.rupeng.pojo.Subject;
import com.rupeng.service.CardSubjectService;
import com.rupeng.service.SubjectService;
import com.rupeng.util.AjaxResult;

@Controller
@RequestMapping("/cardSubject")
public class CardSubjectController {
	@Autowired
	private CardSubjectService cardSubjectService;
	@Autowired
	private SubjectService subjectService;
	@RequestMapping(value="/order.do",method=RequestMethod.GET)
	public ModelAndView orderPage(Long subjectId) {
		ModelAndView modelAndView = new ModelAndView("cardSubject/order");
		List<Subject> subjects = subjectService.selectList();
		if (subjectId==null) {
			if (subjects==null || subjects.size()==0) {
				return modelAndView;
			}
			subjectId = subjects.get(0).getId();
		}
		CardSubject cardSubject = new CardSubject();
		cardSubject.setSubjectId(subjectId);
		List<CardSubject> cardSubjects = cardSubjectService.selectList(cardSubject, "seqNum asc");
		List<Card> cards = cardSubjectService.selectFirstListBySecondId(subjectId);
		modelAndView.addObject("subjectId", subjectId);
		modelAndView.addObject("subjects", subjects);
		modelAndView.addObject("cardSubjects", cardSubjects);
		modelAndView.addObject("cards", cards);
		return modelAndView;
	}
	@RequestMapping(value="/order.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult orderSubmit(Long[] cardSubjectIds, Integer[] seqNums) {
		cardSubjectService.order(cardSubjectIds, seqNums);
		return AjaxResult.successInstance("存储学习卡序列成功");
	}
}
